import { getToken } from '@/utils/auth'
import { useUserInfoStore } from '@/store/userInfo'
import { getInfo } from '@/api/user'
import { createRouter, createWebHistory } from 'vue-router'
import Index from '@/views/index.vue'
//中恒e纺智慧云仓
import UserCenter from '@/views/userCenter/index.vue'
import UserList from '@/views/userCenter/userList.vue'
//提货单
import Delivery from '@/views/delivery/index.vue'
//进仓
import WarehouseIn from '@/views/warehouseIn/index.vue'
import WarehouseInAppoint from '@/views/warehouseIn/appoint.vue'
import WarehouseInAdd from '@/views/warehouseIn/add.vue'
import WarehouseInEdit from '@/views/warehouseIn/edit.vue'
import WarehouseInDetail from '@/views/warehouseIn/detail.vue'
import WarehouseInDetailList from '@/views/warehouseIn/detailList.vue'
import WarehouseInBindingDetail from '@/views/warehouseIn/bindingDetail.vue'
//出仓
import WarehouseOut from '@/views/warehouseOut/index.vue'
import WarehouseOutAppoint from '@/views/warehouseOut/appoint.vue'
import WarehouseOutAdd from '@/views/warehouseOut/add.vue'
import WarehouseOutDetail from '@/views/warehouseOut/detail.vue'
import WarehouseOutDetailList from '@/views/warehouseOut/detailList.vue'
import WarehouseOutBindingDetail from '@/views/warehouseOut/bindingDetail.vue'
//过车
import ChangeCar from '@/views/changeCar/index.vue'
import ChangeCarAppoint from '@/views/changeCar/appoint.vue'
import ChangeCarAdd from '@/views/changeCar/add.vue'
import ChangeCarDetail from '@/views/changeCar/detail.vue'
import ChangeCarDetailList from '@/views/changeCar/detailList.vue'
import ChangeCarBindingDetail from '@/views/changeCar/bindingDetail.vue'
//电子宣传册
import Ebrochure from '@/views/ebrochure/index.vue'
// JG管理
import ReleaseManageBindingDetail from '@/views/releaseManage/bindingDetail.vue'
// 运营审批
import ApproveBindingDetail from '@/views/approve/bindingDetail.vue'
// 配送审批
import DeliveryBindingDetail from '@/views/deliveryManage/bindingDetail.vue'
import DeliveryWarehouseInArrange from '@/views/deliveryManage/warehouseInArrange.vue'
import DeliveryCarArrange from '@/views/deliveryManage/carArrange.vue'
import DeliverySelectWarehouse from '@/views/deliveryManage/selectWarehouse.vue'

import Redirect from '@/views/redirect.vue'
import WxBinding from '@/views/wxBinding.vue'
import WxStorageUserBinding from '@/views/wxStorageUserBinding.vue'
import NoBinding from '@/views/noBinding.vue'

import page404 from '@/views/status/404.vue'

import Login from '@/views/login.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Index',
      redirect: '/userCenter',
      component: Index,
      meta: { title: '中恒e纺智慧云仓', isAuth: true, hasNavbar: false },
      children: [],
    },
    {
      path: '/userCenter',
      children: [
        {
          path: '',
          name: 'UserCenter',
          component: UserCenter,
          meta: { title: '中恒e纺智慧云仓', isAuth: true, hasNavbar: false },
        },
        {
          path: '/userCenter/userList',
          name: 'UserList',
          component: UserList,
          meta: { title: '中恒e纺智慧云仓', subTitle: '用户列表', isAuth: false, hasNavbar: true },
        },
      ]
    },
    {
      path: '/delivery',
      children: [{
        path: '',
        name: 'Delivery',
        meta: { title: '司机查单', isAuth: false, hasNavbar: false },
        component: Delivery,
      }]
    },
    {
      path: '/warehouseIn',
      children: [
        // {
        //   path: '',
        //   name: 'WarehouseIn',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '我的进仓', isAuth: true, hasNavbar: true, hasSearch: true },
        //   component: WarehouseIn,
        // },
        // {
        //   path: '/warehouseIn/appoint',
        //   name: 'WarehouseInAppoint',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '预约进仓', isAuth: true, hasNavbar: true },
        //   component: WarehouseInAppoint
        // },
        // {
        //   path: '/warehouseIn/add',
        //   name: 'WarehouseInAdd',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '添加货物', isAuth: true, hasNavbar: true },
        //   component: WarehouseInAdd
        // },
        // {
        //   path: '/warehouseIn/edit',
        //   name: 'WarehouseInEdit',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '修改货物', isAuth: true, hasNavbar: true },
        //   component: WarehouseInEdit
        // },
        // {
        //   path: '/warehouseIn/detail',
        //   name: 'WarehouseInDetail',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '货物详情', isAuth: true, hasNavbar: true },
        //   component: WarehouseInDetail
        // },
        // {
        //   path: '/warehouseIn/detailList',
        //   name: 'WarehouseInDetailList',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '进仓单详情', keepAlive: false, isAuth: true, hasNavbar: true },
        //   component: WarehouseInDetailList
        // },
        {
          path: '/warehouseIn/bindingDetail',
          name: 'WarehouseInBindingDetail',
          meta: { title: '单据详情', isAuth: false, hasNavbar: false },
          component: WarehouseInBindingDetail
        },
      ]
    },
    {
      path: '/warehouseOut',
      children: [
        // {
        //   path: '',
        //   name: 'WarehouseOut',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '我的出仓', isAuth: true, hasNavbar: true, hasSearch: true },
        //   component: WarehouseOut,
        // },
        // {
        //   path: '/warehouseOut/appoint',
        //   name: 'WarehouseOutAppoint',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '预约出仓', isAuth: true, hasNavbar: true },
        //   component: WarehouseOutAppoint
        // },
        // {
        //   path: '/warehouseOut/add',
        //   name: 'WarehouseOutAdd',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '添加货物', isAuth: true, hasNavbar: true, hasSearch: true },
        //   component: WarehouseOutAdd
        // },
        // {
        //   path: '/warehouseOut/detail',
        //   name: 'WarehouseOutDetail',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '货物详情', isAuth: true, hasNavbar: true },
        //   component: WarehouseOutDetail
        // },
        // {
        //   path: '/warehouseOut/detailList',
        //   name: 'WarehouseOutDetailList',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '出仓单详情', keepAlive: false, isAuth: true, hasNavbar: true },
        //   component: WarehouseOutDetailList
        // },
        {
          path: '/warehouseOut/bindingDetail',
          name: 'WarehouseOutBindingDetail',
          meta: { title: '单据详情', isAuth: false, hasNavbar: false },
          component: WarehouseOutBindingDetail
        },
      ]
    },
    {
      path: '/changeCar',
      children: [
        // {
        //   path: '',
        //   name: 'ChangeCar',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '出仓过车', isAuth: true, hasNavbar: true },
        //   component: ChangeCar,
        // },
        // {
        //   path: '/changeCar/appoint',
        //   name: 'ChangeCarAppoint',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '过车办单', isAuth: true, hasNavbar: true },
        //   component: ChangeCarAppoint
        // },
        // {
        //   path: '/changeCar/add',
        //   name: 'ChangeCarAdd',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '添加货物', isAuth: true, hasNavbar: true, hasSearch: true },
        //   component: ChangeCarAdd
        // },
        // {
        //   path: '/changeCar/detail',
        //   name: 'ChangeCarDetail',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '货物详情', isAuth: true, hasNavbar: true },
        //   component: ChangeCarDetail
        // },
        // {
        //   path: '/changeCar/detailList',
        //   name: 'ChangeCarDetailList',
        //   meta: { title: '中恒e纺智慧云仓', subTitle: '过车单详情', keepAlive: false, isAuth: true, hasNavbar: true },
        //   component: ChangeCarDetailList
        // },
        {
          path: '/changeCar/bindingDetail',
          name: 'ChangeCarBindingDetail',
          meta: { title: '单据详情', isAuth: false, hasNavbar: false },
          component: ChangeCarBindingDetail
        },
      ]
    },
    {
      path: '/releaseManage/bindingDetail',
      name: 'ReleaseManageBindingDetail',
      meta: { title: 'JS管理', isAuth: false, hasNavbar: false },
      component: ReleaseManageBindingDetail
    },
    {
      path: '/approve/bindingDetail',
      name: 'ApproveBindingDetail',
      meta: { title: '审批详情', isAuth: false, hasNavbar: false },
      component: ApproveBindingDetail
    },
    {
      path: '/deliveryManage/bindingDetail',
      name: 'DeliveryBindingDetail',
      meta: { title: '详情', isAuth: false, hasNavbar: false },
      component: DeliveryBindingDetail
    },
    {
      path: '/deliveryManage/warehouseInArrange',
      name: 'DeliveryWarehouseInArrange',
      meta: { title: '入仓安排', isAuth: false, hasNavbar: false },
      component: DeliveryWarehouseInArrange
    },
    {
      path: '/deliveryManage/selectWarehouse',
      name: 'DeliverySelectWarehouse',
      meta: { title: '选择仓库', isAuth: false, hasNavbar: false },
      component: DeliverySelectWarehouse
    },
    {
      path: '/deliveryManage/carArrange',
      name: 'DeliveryCarArrange',
      meta: { title: '车辆安排', isAuth: false, hasNavbar: false },
      component: DeliveryCarArrange
    },
    {
      path: '/ebrochure',
      name: 'Ebrochure',
      meta: { title: '中恒e纺企业宣传画册', isAuth: false, hasNavbar: false },
      component: Ebrochure
    },
    {
      path: '/redirect',
      name: 'Redirect',
      meta: { title: '', isAuth: false, hasNavbar: false },
      component: Redirect
    },
    {
      path: '/binding',
      name: 'Binding',
      meta: { title: '微信绑定', isAuth: false, hasNavbar: false },
      component: WxBinding
    },
    {
      path: '/wxStorageUserBinding',
      name: 'WxStorageUserBinding',
      meta: { title: '微信绑定', isAuth: false, hasNavbar: false },
      component: WxStorageUserBinding
    },
    {
      path: '/noBinding',
      name: 'NoBinding',
      meta: { title: '未绑定', isAuth: false, hasNavbar: false },
      component: NoBinding
    },
    {
      path: '/login',
      name: 'Login',
      meta: { title: '登录', isAuth: false, hasNavbar: false },
      component: Login
    },
    {
      path: '/404',
      name: '404',
      meta: { title: '404页面', isAuth: false, hasNavbar: false },
      component: page404
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    (document as any).title = to.meta.title
  }
  //是否需要登录授权的页面
  if (to.meta.isAuth) {
    //是否登录
    if (getToken()) {
      const userInfoStore = useUserInfoStore()
      //是否拉取用户信息
      if (userInfoStore.roles.length === 0) {
        getInfo().then((res: any) => {
          userInfoStore.permissions = res.permissions
          userInfoStore.roles = res.roles
          next()
        })
      } else {
        next()
      }
    } else {
      next('/userCenter/userList')
    }
  } else {
    next()
  }
})

export default router
